// 执行更新libs

const fs = require('fs')
const path = require('path')
const shell = require('shelljs'); //shelljs 更强大

const StaticPath = path.join(__dirname, '../../../../static')
const PublicPath = path.join(__dirname, '../../../../public/static')

module.exports = function() {
    return new Promise((resolve) => {
        // 更新lebs
        fs.access(StaticPath, fs.constants.F_OK, (err) => {
            console.log('\n\x1b[33m请稍后, 正在执行libs更新...\n')
            let target = path.join(__dirname, '../../_libs')
            let dest

            // 设置需要拷贝到的目标路径
            err ? dest = PublicPath : dest = StaticPath

            // 先执行删除
            fs.access(dest + '/libs', fs.constants.F_OK, (err) => {
                if (!err) {
                    shell.rm('-rf', dest + '/libs');
                }
            })

            setTimeout(() => {
                shell.cp('-R', target, dest);
                // 重命名目标路径
                shell.mv(dest + '/_libs', dest + '/libs')
                resolve()
            }, 1000)
        })
    })
}
